package org.greyhawk.samonitor.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.greyhawk.samonitor.domain.Resources;
import org.greyhawk.samonitor.domain.ResourcesFactory;
import org.greyhawk.samonitor.domain.ResourcesType;
import org.greyhawk.samonitor.domain.Site;
import org.greyhawk.samonitor.vo.HtmlSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service
@Scope(value="prototype")
public class ResourcesCrawler {
	
	@Autowired
	private ResourcesFactory imageResourcesFactory;
	@Autowired
	private ResourcesFactory textResourcesFactory;
	
	public ResourcesCrawler() {
		
	}
	
	public SiteCrawler getSiteCrawler(){
		return imageResourcesFactory.getSiteCrawler();
	}
	
	public ResourcesCrawler(ResourcesFactory imageResourcesFactory,ResourcesFactory textResourcesFactory){
		this.imageResourcesFactory = imageResourcesFactory;
		this.textResourcesFactory = textResourcesFactory;
	}
	
	public void execute(){
			if(hasSite())
			{
				List<Site> sites = this.getAllSite();
				for (Site site : sites) {
					try{
						HtmlSource source = getSiteCrawler().crawSource(site.getUrlPath());
						List<String> imagePaths = this.analizyImagePath(source.getContent());
						this.persistResources(site, ResourcesType.I, imagePaths);
						List<String> flashPaths = this.analizyFlashPath(source.getContent());
						this.persistResources(site, ResourcesType.F, flashPaths);
						List<String> texts = this.analizyTextPath(source);
						this.persisText(site, ResourcesType.T, texts);
					}
					catch(Exception e){
						continue;
					}
					
				}
			}
			else
			{
				System.out.println("No Site to Crawler");
			}
	}
	
	public void exceute(List<String> urlPaths){
		for(String path:urlPaths){
			try{
				HtmlSource source = getSiteCrawler().crawSource(path);
				this.analizyImagePath(source.getContent());
				this.analizyFlashPath(source.getContent());
				this.analizyTextPath(source);
			}
			catch(Exception e){
				continue;
			}
			
		}
	}
	
	public void persistResources(Site site,ResourcesType type,List<String> imagePaths){
		for (String sourcePath : imagePaths) {
			Thread thread = new Thread(new ResourcesCrawlerRunable(site, type, imageResourcesFactory, sourcePath,new FileGenerater()));
			thread.start();
			}
		}
	
	public void persisText(Site site,ResourcesType type,List<String> texts){
		for (String sourcePath : texts) {
			Thread thread = new Thread(new ResourcesCrawlerRunable(site, type, textResourcesFactory, sourcePath));
			thread.start();
		}
	}
	
	public boolean hasSite(){
		long count = Site.countSites();
		if(count == 0)
			return false;
		else
			return true;
	}
	
	public List<Site> getAllSite(){
		List<Site> siteList = Site.findAllSites();
		return siteList;
	}
	
	public List<String> analizyFlashPath(byte[] htmlData)throws Exception{
		List<String> list = new ArrayList<String>();
		list = getSiteCrawler().parseFlash(htmlData);	
		return list;
	}
	
	public List<String> analizyImagePath(byte[] htmlDate)throws Exception{
		List<String> list = new ArrayList<String>();
		list = getSiteCrawler().parseImage(htmlDate);
		return list;
	}
	
	
	public List<String> analizyTextPath(HtmlSource source)throws Exception{
		List<String> list = new ArrayList<String>();
		list = getSiteCrawler().parseText(source);
		return list;
	}
	
	public boolean hasCrawler(String summary){
		Query query = Resources.findResourcesesBySummary(summary);
		int count = query.getResultList().size();
		if(count > 0)
			return true;
		else
			return false;
	}
	
	

}
